/**
 * 
 */
package cn.edu.bjtu.alex.rewrite.tools;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.common.collect.Sets;

import cn.edu.bjtu.alex.rewrite.Context;
import cn.edu.bjtu.alex.rewrite.components.SelectFeature;
import cn.edu.bjtu.alex.rewrite.interfaces.TermFeatureable;
import cn.edu.bjtu.alex.rewrite.tools.SortUtils.Result;
/**
 * @author alex
 *
 */
public class SimpleMergeTermsPolicy implements MergeTermsPolicy {

	SelectFeature sf;
	public SimpleMergeTermsPolicy(SelectFeature sf) {
		this.sf = sf;
	}
	
	public Set<TermFeatureable> merge(Context context, Map<String, Map<String, TermFeatureable>> terms) {
		// merge CHI vectors
		Set<TermFeatureable> mergedTerms = Sets.newHashSet();
		Iterator<Entry<String, Map<String, TermFeatureable>>> chiIter = terms.entrySet().iterator();
		while(chiIter.hasNext()) {
			Entry<String, Map<String, TermFeatureable>> entry = chiIter.next();
			String label = entry.getKey();
			Result sortedResult = sf.getSortedResult(label);
//			LOG.info("Result: label=" + label + ", startIndex=" + sortedResult.getStartIndex() + ", endIndex=" + sortedResult.getEndIndex());
			StringBuffer sb = new StringBuffer();
			for (int i = sortedResult.getStartIndex(); i <= sortedResult.getEndIndex(); i++) {
				Entry<String, TermFeatureable> termEntry = sortedResult.get(i);
				// merge CHI terms for all labels
				mergedTerms.add(termEntry.getValue());
			}
			System.out.println("===========");
//			LOG.debug("label=" + label + ", terms=" + sb.toString().trim());
		}
//		LOG.info("Merged terms: count=" + mergedTerms.size());
		return mergedTerms;
	}


}